Creating Variables for AHK Scripts

Date: 2011-03-08
Author: Sean Gibson

Continuing our discussion on how to program AHK scripts, we’ll talk about some of the basics behind variables and expressions. AutoHotKey (AHK) does not have explicitly defined variable types, but a variable containing only digits is auto-interpreted as a number when a math operation or comparison requires it.

With the exception of local variables (which are accessed or created inside a function), all variables are global and their contents may be read or altered by any part of a script you create.  Remember that except where noted on the functions page, variables are not declared and only are created by using them.

Variable names in AHK scripts are not case sensitive, so something like "PokerTableName" is the same as "pokertablename."  All variable names can be up to 253 characters long and can be numbers, letters, and allowed punctuation characters.  Style conventions within the AHK community call for variables to be named only using letters, numbers, and the underscore character, so something like "Mouse_Position" is fine, while "Mouse[Position]" isn’t.

Going this route will allow others to take a look at your script if you run into a problem and understand the variables you used.  Although a variable can just be numbers, the style is typically reserved for incoming command line parameters.  Also, the words “and,” “or,” and “not” should not be used in variable names.

To store a string or number inside a variable, you can use either the traditional or expression method.  The traditional method uses the equal sign operator to assign unquoted literal strings or variables.  An example of this would be:

SomeNumber = 431
SomeString = “This is a literal string.”
CopyOfVar = %Var%

On the other hand, you can use the expression method, which uses the colon-equal operator (:=) to store numbers, quoted strings, and other expressions.  Here’s an example of this method:

SomeNumber := 552
SomeString := “This is another literal string.”
CopyOfVar := Var

You’ll notice that with this operator, the percent signs are not used.  Now, as you might have surmised, there are two different ways to erase the contents of a variable.

MyVar =
MyVar := “”

The empty quotes are only used with the expression method.  If you used them with the traditional method, they would store two literal quote characters inside the variable.

Now, let’s say you wanted to retrieve the contents of variables you create.  Like the two ways of storing variables, there are two ways of retrieving them.  The traditional method requires that each variable name be enclosed in percent signs when you want to retrieve the contents.  An example:

MsgBox The value in the variable named Var is %Var%
CopyOfVar = %Var%

The expression method takes out the percent signs around the variable names, but encloses literal strings in quotes.  Here’s an example of this method:

MsgBox % “The value in the variable named Var is “ . Var . “.”
CopyOfVar := Var

In the MsgBox line for the expression method, a percentage sign and space were used to change the parameter from traditional to expression mode.  We do this because the traditional method is used by default for all commands.  But, there are certain parameters of some commands that are documented as accepting expressions, in which case the leading percent sign is permitted, but not necessary.

At this point, we’ll move our discussion to expressions, which are used to perform one or more operations on a series of variables, literal strings, or literal numbers.  Variable names inside expressions are not enclosed in percent signs and, therefore, literal strings must be enclosed in double quotes to distinguish them.  Here’s an example:

if (CurrentSetting > 100 or SomeColor <> “Green”)
MsgBox The setting is too high or the wrong color is up.

In this example, “Green” is in quotes because it is a literal string.  To put in an actual quote-character inside a literal string, specify two consecutive quotes as shown twice in this example: “Barry said, “”For every bad beat.”””

Note that an if-statement containing an expression is differentiated from a traditional if-statement such as If FoundColor <> Green by making the character after the word if an open parenthesis.  Although this is usually done by putting the entire expression in parenthesis, it can be done by doing something like: if (x > 0) and (y > 0).

To specify an empty string inside an expression, use an empty pair of quotes.  For example, if you put something like: If (MyVariable <> “”), it would be true if MyVariable were not blank.  So, to check if a variable is blank with a traditional if, use the equal sign or <> with nothing on the right side.

To assign a result to a variable, use the := operator.  Here’s an example:

NetProfit := Profit * (1 – Gross/100)

When an expression is required to evaluate to be true or false such as in an if-statement, a blank or zero result is considered to be false and all other results are considered to be true.  Operators such as NOT/AND/OR automatically produce a true or false value where 1 is true and 0 is false.

Inside an expression, numbers are considered to be floating point if they contain a decimal point; otherwise, they are integers.  For most operators, such as additional and multiplication, if either of the inputs is a floating point number, the result will also be a floating point number.

This gives you some background on what it takes to be an AHK script programmer.  Understanding how variables and expressions work is a huge step in planning your script and figuring out how to get it to work properly.


BECOME A MEMBER of PokerSoftware.com today. You can chat with us and ask questions to our poker software experts in the forums and get access to EXCLUSIVE members-only content. Sign up today!


More articles


 

WriteSubmit Your Comment Let us know what you think about this article!





 

Member Comments



 

No comments so far

Like PokerSoftware on Facebook and Get the Latest Software News Plus Weekly Prizes!

recomented-softwares Recommended Software

Tracking Analysis

SNG Tools

Training